package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCDemo8 {
    public static void main(String[] args) {
        try (
                Connection conn = DBUtil.getConnection();)
        {
            System.out.println("与数据库连接成功！");
            String sql = "SELECT COUNT(*) count,c.name\n" +
                         "FROM class c \n" +
                         "JOIN student s ON c.id = s.class_id \n" +
                         "GROUP BY c.name ";
            Statement stat = conn.createStatement();
            ResultSet rs = stat.executeQuery(sql);
            while(rs.next()){
                int count = rs.getInt("count");
                String className = rs.getString("c.name");
                System.out.println("人数："+count + "\t班级：" + className+ "\t");
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
